<?php

require(dirname(__FILE__) . "/../../../../library/phpmailer/class.phpmailer.php");

class MRSendMail {
	
	public static $MAIL = "mail";
	public static $SENDMAIL = "sendmail";
	public static $SMTP = "smtp";

	private $server = "";
	private $port = "";

	private $method = "asdf";
	private $authentication = "";
	private $protocol = "";
	private $fromName = "";
	private $fromEmail = "";
	private $fromPass = "";

	private $replyToName = "";
	private $replyToEmail = "";

	private function MR_Mail($email_subject, $email_body, $to, $cc, $bcc) {

		$mail = new PHPMailer(true);
		$mail->CharSet = "UTF-8";

		try {

			$mail->SetFrom($this->fromEmail, $this->fromName);
			$mail->AddReplyTo($this->replyToEmail, $this->replyToName);

			if (!empty($to)) {
				$toArray = split(",", $to);
				foreach($toArray as $toValue) {
					$mail->AddAddress(trim($toValue));
				}
				$mail->SingleTo = true;
			}

			if (!empty($cc)) {
				$ccArray = split(",", $cc);
				foreach($ccArray as $ccValue) {
					$mail->AddCC(trim($cValue));
				}
			}

			if (!empty($bcc)) {
				$bccArray = split(",", $bcc);
				foreach($bccArray as $bccValue) {
					$mail->AddBCC(trim($bccValue));
				}
			}

			$mail->Subject = $email_subject;
			//$mail->Body = $email_body;
			$mail->MsgHTML(nl2br($email_body));
			$mail->Send();

		} catch (phpmailerException $e) {
			echo $e->errorMessage();
		} catch (Exception $e) {
			echo $e->getMessage();
		}

		unset($mail);
	}

	private function MR_Sendmail($email_subject, $email_body, $to, $cc, $bcc) {

		$mail = new PHPMailer(true);
		$mail->IsSendmail();
		$mail->CharSet = "UTF-8";

		try {
			$mail->SetFrom($this->fromEmail, $this->fromName);
			$mail->AddReplyTo($this->replyToEmail, $this->replyToName);

			if (!empty($to)) {
				$toArray = split(",", $to);
				foreach($toArray as $toValue) {
					$mail->AddAddress(trim($toValue));
				}
				$mail->SingleTo = true;
			}

			if (!empty($cc)) {
				$ccArray = split(",", $cc);
				foreach($ccArray as $ccValue) {
					$mail->AddCC(trim($cValue));
				}
			}

			if (!empty($bcc)) {
				$bccArray = split(",", $bcc);
				foreach($bccArray as $bccValue) {
					$mail->AddBCC(trim($bccValue));
				}
			}

			$mail->Subject = $email_subject;
			//$mail->Body = $email_body;
			$mail->MsgHTML(nl2br($email_body));

			$mail->Send();

		} catch (phpmailerException $e) {
			echo $e->errorMessage();
		} catch (Exception $e) {
			echo $e->getMessage();
		}

		unset($mail);
	}

	private function MR_SMTP($email_subject, $email_body, $to, $cc, $bcc) {

		$mail = new PHPMailer(true);
		$mail->IsSMTP();
		$mail->CharSet = "UTF-8";

		try {
				
			$mail->Host = $this->server;
			$mail->Port = $this->port;

			if ($this->authentication == "yes") {
				$mail->SMTPAuth   = true;
				$mail->SMTPSecure = $this->protocol;
				$mail->Username   = $this->fromEmail;
				$mail->Password   = $this->fromPass;
			}
				
			$mail->SetFrom($this->fromEmail, $this->fromName);
			$mail->AddReplyTo($this->replyToEmail, $this->replyToName);
				
			if (!empty($to)) {
				$toArray = split(",", $to);
				foreach($toArray as $toValue) {
					$mail->AddAddress(trim($toValue));
				}
				$mail->SingleTo = true;
			}
				
			if (!empty($cc)) {
				$ccArray = split(",", $cc);
				foreach($ccArray as $ccValue) {
					$mail->AddCC(trim($ccValue));
				}
			}
				
			if (!empty($bcc)) {
				$bccArray = split(",", $bcc);
				foreach($bccArray as $bccValue) {
					$mail->AddBCC(trim($bccValue));
				}
			}


			error_log("Subject: " . $email_subject . "\n\nBody:\n" . $email_body . "\r\n\n\n", 3, dirname(__FILE__) . "/../../../../log/log.txt");
			
			$mail->Subject = $email_subject;
			$mail->MsgHTML(nl2br($email_body));
			$mail->Send();
				
		} catch (phpmailerException $e) {
			echo $e->errorMessage();
		} catch (Exception $e) {
			echo $e->getMessage();
		}

		unset($mail);
	}



	private function readEmailParameters() {
		$this->server = plugin_config_get('server');
		$this->port = plugin_config_get('port');

		$this->method = plugin_config_get('method');
		$this->authentication = plugin_config_get('authentication');
		$this->protocol = plugin_config_get('protocol');
		$this->fromName = plugin_config_get('from_name');
		$this->fromEmail = plugin_config_get('from_email');
		$this->fromPass = plugin_config_get('from_pass');

		$this->replyToName = plugin_config_get('replyto_name');
		$this->replyToEmail = plugin_config_get('replyto_email');
	}

	public function sendEmail($email_subject, $email_body, $to = "", $cc = "", $bcc = "") {
		
		if (!$this->checkParametersFilled($email_body) || !$this->checkParametersFilled(false, $to, $cc, $bcc)) {
			return;
		}

		$this->readEmailParameters();

		if ($this->method == MRSendMail::$MAIL && $this->checkParametersFilled($this->fromEmail, $this->replyToEmail)) {
			$this->MR_Mail($email_subject, $email_body, $to, $cc, $bcc);
		} elseif ($this->method == MRSendMail::$SENDMAIL && $this->checkParametersFilled($this->fromEmail, $this->replyToEmail)) {
				$this->MR_Sendmail($email_subject, $email_body, $to, $cc, $bcc);
		} elseif ($this->method == MRSendMail::$SMTP && $this->checkParametersFilled($this->server, $this->port, $this->fromEmail, $this->replyToEmail)) {
				$this->MR_SMTP($email_subject, $email_body, $to, $cc, $bcc);
		}

	}

	private function checkParametersFilled($allToTrue = true) {

		$parameters = array_slice(func_get_args(), 1);

		if ($allToTrue == true) {
				
			foreach ($parameters as $param) {
				if (empty($param)) {
					return false;
				}
			}
				
			return true;
		} else {
			
			foreach ($parameters as $param) {
				if (!empty($param)) {
					return true;
				}
			}
			
			return false;
		}


	}
}